#coding:utf-8
from conf import Conf
from KeyValueTable import Keyvaluetable
from common import *
import re
from loguru import logger
class Analyzetablepre():
    def __init__(self,**params):
        self.params1 = params
    #单表表内提取
    def solo_table_extract(self,table_dict):
        out_dict = {}
        for key in table_dict:
            now_dict = {
                        key:
                            {
                            'value':[],
                            'x':[],
                            'y':[],
                            'standard':[],
                            'prior':[]
                            }
                        }
            now_data = table_dict[key]
            temp_ls_num = []
            for num,num_data in enumerate(now_data):
                temp_ls_num.append(num)
            map_key = {
                       0:'value',
                       1:'x',
                       2:'y',
                       3:'standard',
                       4:'prior'
                       }
            for num,num_data in enumerate(temp_ls_num):
                tt_data = now_data[num_data]
                for num1 in range(5):
                    key_use = map_key[num1]
                    now_dict[key][key_use].append(tt_data[num1])
            out_dict.update(now_dict)
        return out_dict

    #多表表内提取
    def mul_table_extract(self,pub_data):
        out_ls = []
        for table_dict in pub_data:
            solo_dict = self.solo_table_extract(table_dict)
            out_ls.append(solo_dict)
        return out_ls

    #关键字的优先级
    def key_prior(self,key):
        prior = {'C':['投标(人|单位|企业)','-1008600'],
                 'A':['第一(中标)?排序(人|单位名称)','-1008600'],
                 'B':['|','-1008600']}
        for prior_key in prior:
            if re.search(prior[prior_key][0],key) != None and re.search(prior[prior_key][1],key) == None:
                return prior_key

    def add_prior(self,pub_data):
        for table_dict in pub_data:
            for key in table_dict:
                for num in range(len(table_dict[key])):
                    table_dict[key][num].append(self.key_prior(key))
        return pub_data

    def main(self):
        #张俊辉的数据
        pub_data,is_key = Keyvaluetable(**self.params1).main()
        pub_data = self.add_prior(pub_data)
        #提取的数据
        mul_ls = self.mul_table_extract(pub_data)
        return mul_ls

if __name__ == '__main__':
    with open(r'C:\Users\Administrator\Desktop\table_easy\table1.html',encoding='utf-8') as f:
        html = f.read()
    with Db('dispose') as conn:
        cursor = conn.cursor()
        params = {
                'conn':conn,
                'cursor':cursor
                  }
        conf = Conf(**params).main()
        params = {
                'conf':conf,
                'html':html
                }
        a = Analyzetablepre(**params).main()
        print(a)